package com.center.growcenterserver.controller;


import com.center.growcenterserver.common.R;
import com.center.growcenterserver.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 课程的控制类，用于提交课程评分以及查询课程信息
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseRatingService;

    /**
     * 提交课程评分
     */
    @PostMapping("/rate/{courseId}/{studentId}")
    public R rateCourse(@PathVariable("courseId") String courseId,
                        @PathVariable("studentId") String studentId,
                        @RequestBody Map<String, Object> requestBody) {
        Object ratingObj = requestBody.get("rating");
        Double rating;

        if (ratingObj instanceof Integer) {
            rating = ((Integer) ratingObj).doubleValue();
        } else if (ratingObj instanceof Double) {
            rating = (Double) ratingObj;
        } else {
            return R.error("无效的评分格式");
        }

        if (rating == null) {
            return R.error("评分参数缺失");
        }
        return courseRatingService.submitRating(courseId, studentId,rating);
    }


    /**
     * 查询课程信息
     */
    @GetMapping("/info")
    public R selectCourseInfo() {
        return courseRatingService.selectCourseInfo();
    }


}
